package com.hyfrogx.modules.manage.util;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyfrogx.modules.manage.entity.task.TaskRewardEntity;
import com.hyfrogx.modules.manage.entity.task.TaskUserInfoEntity;
import com.hyfrogx.modules.manage.service.task.TaskUserInfoService;

import java.util.*;
/**
 * @author 罗春杰-技术部
 */
public class TaskRewardUtils {

    /**
     * 从奖项集合中随机出一个奖项
     * @param list
     * @return HyTaskReward
     * @Description: (这里用一句话描述这个方法的作用)
     * @throws
     */
    //无法装配暂时先当参数引入
    public static TaskRewardEntity getRewardSingle(List<TaskRewardEntity> list, TaskUserInfoService userInfoService,
                                                   Integer uid) {
        Collections.sort(list, new Comparator<TaskRewardEntity>() {
            @Override
            public int compare(TaskRewardEntity o1, TaskRewardEntity o2) {
                // 升序
                return o1.getChance().compareTo(o2.getChance());
            }
        });
        Integer randomNumber = TaskRewardUtils.getRandomNumber(list);
        TaskRewardEntity hyTaskReward = list.get(randomNumber);
        List<TaskUserInfoEntity> taskInfoReward = userInfoService.list(new QueryWrapper<TaskUserInfoEntity>()
                .eq("uid",uid).eq("reward_id",hyTaskReward.getId()));
        if (hyTaskReward.getSingle() == 1 && CollUtil.isNotEmpty(taskInfoReward)) {
            return TaskRewardUtils.getRewardSingle(list, userInfoService, uid);
        } else {
            return hyTaskReward;
        }
    }


    //新手任务的概率
    public static TaskRewardEntity getReward(List<TaskRewardEntity> list) {
        Collections.sort(list, new Comparator<TaskRewardEntity>() {
            @Override
            public int compare(TaskRewardEntity o1, TaskRewardEntity o2) {
                // 升序
                return o1.getChance().compareTo(o2.getChance());
            }
        });
        Integer randomNumber = TaskRewardUtils.getRandomNumber(list);
        TaskRewardEntity hyTaskReward = list.get(randomNumber);
        if (hyTaskReward.getSingle() == 1) {
            TaskRewardUtils.getReward(list);
        }
        return hyTaskReward;
    }
    /**
     * 将数据库中的概率转换
     * @param list
     * @return
     */
    public static Integer getRandomNumber(List<TaskRewardEntity> list) {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        Integer num = 0;
        for (int i = 0; i < list.size(); i++) {
            TaskRewardEntity hyTaskReward = list.get(i);
            if (i == 0) {
                num = hyTaskReward.getChance();
                map.put(0, num);
                continue;
            }
            num = num + hyTaskReward.getChance();
            map.put(num - hyTaskReward.getChance(), num);
            if (i == (list.size() - 1)) {
                map.put(num - hyTaskReward.getChance(), 100);
            }
        }

        int choice = (int) (Math.random() * 100);
        int nums = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (choice > entry.getKey() && choice <= entry.getValue()) {
                return nums;
            }
            nums++;
        }

        return null;
    }


}
